package com.teacherprofile.common.converters.refresher_trainings;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.teacherprofile.common.constant.CacheConstants;
import com.teacherprofile.common.core.domain.entity.SysDictData;

import java.util.List;
import java.util.Objects;

import static com.teacherprofile.common.utils.DictUtils.getDictCache;

/**
 * 进修培训成果转换器
 */
public class RefresherTrainingAchievementsConverter implements Converter<Integer> {
    private static final String ACHIEVEMENTS = CacheConstants.RefresherTraining.ACHIEVEMENTS;
    @Override
    public Class supportJavaTypeKey()
    {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey()
    {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
    {
        String value="";
        String str = cellData.getStringValue();
        List<SysDictData> sysDictDataList =  getDictCache(ACHIEVEMENTS);
        if(sysDictDataList.size() !=0){
            SysDictData sysDictData = sysDictDataList.stream().filter(s -> Objects.equals(s.getDictLabel(), str)).findFirst().orElse(null);
            if(sysDictData !=null){
                value = sysDictData.getDictValue();
            }
        }
        return  Integer.parseInt(value);
    }


    @Override
    public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
    {
        String  str = "未知";
        //        Redis
        List<SysDictData> sysDictDataList =  getDictCache(ACHIEVEMENTS);
        if(sysDictDataList.size() !=0){
            SysDictData sysDictData = sysDictDataList.stream().filter(s -> Objects.equals(s.getDictValue(), value.toString())).findFirst().orElse(null);
            if(sysDictData !=null){
                str = sysDictData.getDictLabel();
            }
        }
        return new WriteCellData(str);
    }
}
